package com.feicheng.iou.service.impl;

import com.feicheng.iou.bean.InformationBean;
import com.feicheng.iou.constant.StatusCodeConstant;
import com.feicheng.iou.entity.ProjectService;
import com.feicheng.iou.exception.SystemExceptionV110;
import com.feicheng.iou.mapper.ProjectServiceMapper;
import com.feicheng.iou.result.ResultInfoV110;
import com.feicheng.iou.service.ProjectServiceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feicheng.iou.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
public class ProjectServiceServiceImpl extends ServiceImpl<ProjectServiceMapper, ProjectService> implements ProjectServiceService {

    @Autowired
    ProjectServiceMapper projectServiceMapper;

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:24 下午 2021/9/1
     */
    @Override
    public ResultInfoV110 selectProjectServiceList() {
        log.info("ProjectServiceServiceImpl 的selectProjectServiceList方法进入");
        List<ProjectService> projectServiceList = projectServiceMapper.selectList(null);
        log.info("ProjectServiceServiceImpl 的selectProjectServiceList方法结束");
        return ResultInfoV110.success(projectServiceList);
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 4:12 下午 2021/9/1
     */
    @Override
    public ResultInfoV110 selectProjectServiceListPage(Integer page, Integer size, ProjectService projectService) {
        log.info("ProjectServiceServiceImpl 的selectProjectServiceListPage方法进入" + projectService);

        Integer pageIndex = PageUtil.getPageIndex(page, size);
        List<ProjectService> projectServiceList = projectServiceMapper.queryProjectServiceList(pageIndex, size, projectService.getName(), projectService.getDes());

        Integer total = projectServiceMapper.queryProjectServiceListCount(projectService);
        Map<String, Integer> pageInfo = PageUtil.getPageInfo(page, size, total);
        HashMap<String, Object> result = new HashMap<>();
        result.put("pageInfo", pageInfo);
        result.put("list", projectServiceList);

        log.info("ProjectServiceServiceImpl 的selectProjectServiceListPage方法结束");
        return ResultInfoV110.success(result);
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:24 下午 2021/9/1
     */
    @Override
    public ProjectService selectProjectServiceByid(Integer id) {
        log.info("ProjectServiceServiceImpl 的selectProjectServiceByid方法进入,参数为" + id);
        log.info("ProjectServiceServiceImpl 的selectProjectServiceByid方法结束");
        return projectServiceMapper.selectById(id);
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:25 下午 2021/9/1
     */
    @Override
    public ResultInfoV110 insert(ProjectService projectService) {
        log.info("ProjectServiceServiceImpl 的insert方法进入,参数为" + projectService);
        int insertProjectServiceResult = projectServiceMapper.insert(projectService);
        if (insertProjectServiceResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "添加服务项目信息"));
        }
        log.info("ProjectServiceServiceImpl 的insert方法结束");
        return ResultInfoV110.success();
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:25 下午 2021/9/1
     */
    @Override
    public ResultInfoV110 update(ProjectService projectService) {
        log.info("ProjectServiceServiceImpl 的update方法进入,参数为" + projectService);
        int updateProjectServiceResult = projectServiceMapper.updateById(projectService);
        if (updateProjectServiceResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "修改服务项目信息"));
        }
        log.info("ProjectServiceServiceImpl 的update方法结束");
        return ResultInfoV110.success();
    }

    /**
     * @param null
     * @return
     * @Author luoxin
     * @Description TODO
     * @Date 2:25 下午 2021/9/1
     */
    @Override
    public ResultInfoV110 delete(Integer projectServiceId) {
        log.info("ProjectServiceServiceImpl 的delete方法进入,参数为" + projectServiceId);
        int deleteProjectServiceResult = projectServiceMapper.deleteById(projectServiceId);
        if (deleteProjectServiceResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "删除服务项目信息"));
        }
        log.info("ProjectServiceServiceImpl 的delete方法结束");
        return ResultInfoV110.success();
    }

    /**
     * @Author luoxin
     * @Description 服务项目开关修改
     * @Date 9:10 上午 2021/9/2
     * @param null
     * @return
     */
    @Override
    public ResultInfoV110 updateStatus(Integer projectServiceId, boolean status) {
        log.info("ProjectServiceServiceImpl 的updateStatus方法进入,参数为" + projectServiceId+"状态为"+status);

        ProjectService projectService = new ProjectService();
        projectService.setId(projectServiceId);
        projectService.setStatus(status);
        int updateProjectServiceResult = projectServiceMapper.updateById(projectService);
        if (updateProjectServiceResult == 0) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F001, "修改服务项目信息"));
        }

        log.info("ProjectServiceServiceImpl 的updateStatus方法结束" );
        return ResultInfoV110.success();
    }
}
